//////////Change the directory before running the code
/////////// Panel A: event-level variables
cd "D:\data&code"
//// Load voting dataset 
use pseudo_dataset/voting, clear
keep if contest == 1

//// Stats for event-level variables
bysort pf_no: egen for_diss_event = mean(for_diss) 
by     pf_no : keep if _n == 1

keep pf_no diss_win for_diss_event iss_for gl_for hf no_camp share_intensity announce_return

tempfile all
save `all', replace

clear
tempfile collector
save `collector', emptyok replace

use `all', clear
save `all', replace

foreach this_var of varlist diss_win for_diss_event iss_for gl_for hf no_camp share_intensity announce_return {

	// Load file
	use `all', clear
	save `all', replace
	
	// keep this variable
	keep `this_var'

	// mean, median, sd
	egen var_mean   = mean(`this_var')
	egen var_median = median(`this_var')
	egen var_sd     = sd(`this_var')
	drop `this_var'
	
	// label variable
	gen covariate = "`this_var'"
	
	keep if _n == 1
	
	// append
	append using `collector'
	save `collector', replace
}

///// Which cutoff to use
gen cutoff = .
replace cutoff = 1 			if covariate == "iss_for"
replace cutoff = 1 			if covariate == "gl_for"
replace cutoff = 1 			if covariate == "hf"
replace cutoff = var_median if covariate == "no_camp"
replace cutoff = var_median if covariate == "share_intensity"
replace cutoff = var_median if covariate == "announce_return"

order covariate cutoff

////// Save
tempfile cutoffs
save `cutoffs', replace
	
//////// get support
clear
tempfile collector
save `collector', emptyok replace

use pseudo_dataset/voting, clear

foreach this_var of varlis iss_for gl_for hf no_camp share_intensity announce_return {

	//// Load voting dataset
	use pseudo_dataset/voting, clear
	keep if contest == 1
	
	//// Keep what we need
	keep pf_no `this_var' vote_num
	drop if `this_var' == .
	
	//// Average votes of each type within event
	mark fm if vote_num == 0
	mark pm if vote_num == 0.25
	mark ab if vote_num == 0.5
	mark pd if vote_num == 0.75
	mark fd if vote_num == 1
			
	///// Get cutoff value to use
	gen covariate = "`this_var'"
	merge m:1 covariate using `cutoffs'
	keep if _merge == 3 
	drop _merge
	
	///// Separate events
	gen level = ""
	replace level = "high" if `this_var' >= cutoff
	replace level = "low"  if `this_var' < cutoff
	drop `this_var'
	drop cutoff
	drop pf_no
	drop vote_num
	
	///// Means within each level
	bysort level : egen level_fm = mean(fm)
	bysort level : egen level_pm = mean(pm)
	bysort level : egen level_ab = mean(ab)
	bysort level : egen level_pd = mean(pd)
	bysort level : egen level_fd = mean(fd)
	
	drop fm pm ab pd fd
	
	by level : keep if _n == 1	
	
	////// Reshape by level
	reshape wide level_fm level_pm level_ab level_pd level_fd, i(covariate var_mean var_sd) j(level) string
	
	rename level_fmhigh FM_high
	rename level_fmlow  FM_low
	rename level_pmhigh PM_high
	rename level_pmlow  PM_low
	rename level_abhigh AB_high
	rename level_ablow  AB_low
	rename level_pdhigh PD_high
	rename level_pdlow  PD_low
	rename level_fdhigh FD_high
	rename level_fdlow  FD_low
	
	order covariate var_mean var_median var_sd FM_low FM_high PM_low PM_high AB_low AB_high PD_low PD_high FD_low FD_high
	
	////// Append
	append using `collector'
	save `collector', replace
}

append using `cutoffs'
drop if FM_low == . & !inlist(covariate,"for_diss_event","diss_win")

tempfile panel_a
save `panel_a', replace

/////////////// Panel B: fund-level variables

///// Passive
{
use pseudo_dataset/voting, clear
keep if contest == 1
drop if passive == .

keep passive vote_num
gen covariate = "passive"

egen var_mean    = mean(passive)
egen var_median  = median(passive)
egen var_sd      = sd(passive)

gen level = ""
replace level = "high" if passive == 1
replace level = "low"  if passive == 0

mark fm if vote_num == 0
mark pm if vote_num == 0.25
mark ab if vote_num == 0.5
mark pd if vote_num == 0.75
mark fd if vote_num == 1	

drop vote_num passive

sort level
by   level : egen level_fm = mean(fm)
by   level : egen level_pm = mean(pm)
by   level : egen level_ab = mean(ab)	
by   level : egen level_pd = mean(pd)
by   level : egen level_fd = mean(fd)

drop fm pm ab pd fd

by level : keep if _n == 1
reshape wide level_fm level_pm level_ab level_pd level_fd, i(covariate var_mean var_sd) j(level) string

rename level_fmhigh FM_high
rename level_fmlow  FM_low
rename level_pmhigh PM_high
rename level_pmlow  PM_low
rename level_abhigh AB_high
rename level_ablow  AB_low
rename level_pdhigh PD_high
rename level_pdlow  PD_low
rename level_fdhigh FD_high
rename level_fdlow  FD_low
	
order covariate var_mean var_median var_sd FM_low FM_high PM_low PM_high AB_low AB_high PD_low PD_high FD_low FD_high
			
tempfile passive
save `passive', replace
}

///// Fund assets
{
use pseudo_dataset/voting, clear
keep if contest == 1
drop if port_val == .

keep port_val vote_num
gen covariate = "port_val"

egen var_mean    = mean(port_val)
egen var_median  = median(port_val)
egen var_sd      = sd(port_val)

gen level = ""
replace level = "high" if port_val >= var_median
replace level = "low"  if port_val <  var_median

mark fm if vote_num == 0
mark pm if vote_num == 0.25
mark ab if vote_num == 0.5
mark pd if vote_num == 0.75
mark fd if vote_num == 1	

drop vote_num port_val

sort level
by   level : egen level_fm = mean(fm)
by   level : egen level_pm = mean(pm)
by   level : egen level_ab = mean(ab)	
by   level : egen level_pd = mean(pd)
by   level : egen level_fd = mean(fd)

drop fm pm ab pd fd

by level : keep if _n == 1
reshape wide level_fm level_pm level_ab level_pd level_fd, i(covariate var_mean var_sd) j(level) string

rename level_fmhigh FM_high
rename level_fmlow  FM_low
rename level_pmhigh PM_high
rename level_pmlow  PM_low
rename level_abhigh AB_high
rename level_ablow  AB_low
rename level_pdhigh PD_high
rename level_pdlow  PD_low
rename level_fdhigh FD_high
rename level_fdlow  FD_low
	
order covariate var_mean var_median var_sd FM_low FM_high PM_low PM_high AB_low AB_high PD_low PD_high FD_low FD_high
			
replace var_mean 	= var_mean/1000000000 	
replace var_median 	= var_median/1000000000 
replace var_sd 		= var_sd/1000000000 					
			
tempfile port_val
save `port_val', replace
}	

///// % of fund assets
{
use pseudo_dataset/voting, clear
keep if contest == 1
drop if port_share == .

keep port_share vote_num
gen covariate = "port_share"

egen var_mean    = mean(port_share)
egen var_median  = median(port_share)
egen var_sd      = sd(port_share)

gen level = ""
replace level = "high" if port_share >= var_median
replace level = "low"  if port_share <  var_median

mark fm if vote_num == 0
mark pm if vote_num == 0.25
mark ab if vote_num == 0.5
mark pd if vote_num == 0.75
mark fd if vote_num == 1	

drop vote_num port_share

sort level
by   level : egen level_fm = mean(fm)
by   level : egen level_pm = mean(pm)
by   level : egen level_ab = mean(ab)	
by   level : egen level_pd = mean(pd)
by   level : egen level_fd = mean(fd)

drop fm pm ab pd fd

by level : keep if _n == 1
reshape wide level_fm level_pm level_ab level_pd level_fd, i(covariate var_mean var_sd) j(level) string

rename level_fmhigh FM_high
rename level_fmlow  FM_low
rename level_pmhigh PM_high
rename level_pmlow  PM_low
rename level_abhigh AB_high
rename level_ablow  AB_low
rename level_pdhigh PD_high
rename level_pdlow  PD_low
rename level_fdhigh FD_high
rename level_fdlow  FD_low
	
order covariate var_mean var_median var_sd FM_low FM_high PM_low PM_high AB_low AB_high PD_low PD_high FD_low FD_high
							
tempfile port_share
save `port_share', replace
}	


///// % of firm equity
{
use pseudo_dataset/voting, clear
keep if contest == 1
drop if own_out == .

keep own_out vote_num
gen covariate = "own_out"

egen var_mean    = mean(own_out)
egen var_median  = median(own_out)
egen var_sd      = sd(own_out)

gen level = ""
replace level = "high" if own_out >= var_median
replace level = "low"  if own_out <  var_median

mark fm if vote_num == 0
mark pm if vote_num == 0.25
mark ab if vote_num == 0.5
mark pd if vote_num == 0.75
mark fd if vote_num == 1	

drop vote_num own_out

sort level
by   level : egen level_fm = mean(fm)
by   level : egen level_pm = mean(pm)
by   level : egen level_ab = mean(ab)	
by   level : egen level_pd = mean(pd)
by   level : egen level_fd = mean(fd)

drop fm pm ab pd fd

by level : keep if _n == 1
reshape wide level_fm level_pm level_ab level_pd level_fd, i(covariate var_mean var_sd) j(level) string

rename level_fmhigh FM_high
rename level_fmlow  FM_low
rename level_pmhigh PM_high
rename level_pmlow  PM_low
rename level_abhigh AB_high
rename level_ablow  AB_low
rename level_pdhigh PD_high
rename level_pdlow  PD_low
rename level_fdhigh FD_high
rename level_fdlow  FD_low
	
order covariate var_mean var_median var_sd FM_low FM_high PM_low PM_high AB_low AB_high PD_low PD_high FD_low FD_high
							
tempfile own_out
save `own_out', replace
}		

///// holding horizon
{
use pseudo_dataset/voting, clear
keep if contest == 1
drop if horizon == .

keep horizon vote_num
gen covariate = "horizon"

egen var_mean    = mean(horizon)
egen var_median  = median(horizon)
egen var_sd      = sd(horizon)

gen level = ""
replace level = "high" if horizon >= var_median
replace level = "low"  if horizon <  var_median

mark fm if vote_num == 0
mark pm if vote_num == 0.25
mark ab if vote_num == 0.5
mark pd if vote_num == 0.75
mark fd if vote_num == 1	

drop vote_num horizon

sort level
by   level : egen level_fm = mean(fm)
by   level : egen level_pm = mean(pm)
by   level : egen level_ab = mean(ab)	
by   level : egen level_pd = mean(pd)
by   level : egen level_fd = mean(fd)

drop fm pm ab pd fd

by level : keep if _n == 1
reshape wide level_fm level_pm level_ab level_pd level_fd, i(covariate var_mean var_sd) j(level) string

rename level_fmhigh FM_high
rename level_fmlow  FM_low
rename level_pmhigh PM_high
rename level_pmlow  PM_low
rename level_abhigh AB_high
rename level_ablow  AB_low
rename level_pdhigh PD_high
rename level_pdlow  PD_low
rename level_fdhigh FD_high
rename level_fdlow  FD_low
	
order covariate var_mean var_median var_sd FM_low FM_high PM_low PM_high AB_low AB_high PD_low PD_high FD_low FD_high
							
tempfile horizon
save `horizon', replace
}		

///// basis return
{
use pseudo_dataset/voting, clear
keep if contest == 1
drop if basis_ret == .

keep basis_ret vote_num
gen covariate = "basis_ret"

egen var_mean    = mean(basis_ret)
egen var_median  = median(basis_ret)
egen var_sd      = sd(basis_ret)

gen level = ""
replace level = "high" if basis_ret >= var_median
replace level = "low"  if basis_ret <  var_median

mark fm if vote_num == 0
mark pm if vote_num == 0.25
mark ab if vote_num == 0.5
mark pd if vote_num == 0.75
mark fd if vote_num == 1	

drop vote_num basis_ret

sort level
by   level : egen level_fm = mean(fm)
by   level : egen level_pm = mean(pm)
by   level : egen level_ab = mean(ab)	
by   level : egen level_pd = mean(pd)
by   level : egen level_fd = mean(fd)

drop fm pm ab pd fd

by level : keep if _n == 1
reshape wide level_fm level_pm level_ab level_pd level_fd, i(covariate var_mean var_sd) j(level) string

rename level_fmhigh FM_high
rename level_fmlow  FM_low
rename level_pmhigh PM_high
rename level_pmlow  PM_low
rename level_abhigh AB_high
rename level_ablow  AB_low
rename level_pdhigh PD_high
rename level_pdlow  PD_low
rename level_fdhigh FD_high
rename level_fdlow  FD_low
	
order covariate var_mean var_median var_sd FM_low FM_high PM_low PM_high AB_low AB_high PD_low PD_high FD_low FD_high
							
tempfile basis_ret
save `basis_ret', replace
}		

////// Append everything together
use `panel_a', clear
append using `passive'
append using `port_val'
append using `port_share'
append using `own_out'
append using `horizon'
append using `basis_ret'

////// Sort
gen rank = .
replace rank = 1 if covariate == "diss_win"
replace rank = 2 if covariate == "for_diss_event"
replace rank = 3 if covariate == "iss_for"
replace rank = 4 if covariate == "gl_for"
replace rank = 5 if covariate == "hf"
replace rank = 6 if covariate == "no_camp"
replace rank = 7 if covariate == "share_intensity"
replace rank = 8 if covariate == "announce_return"	
replace rank = 9 if covariate == "passive"
replace rank = 10 if covariate == "port_val"
replace rank = 11 if covariate == "port_share"
replace rank = 12 if covariate == "own_out"
replace rank = 13 if covariate == "horizon"
replace rank = 14 if covariate == "basis_ret"

sort rank
drop rank
drop cutoff

